<?php
namespace app\front\service\v1;
use app\front\service\v1\ResposeMsg;

class Verify extends ResposeMsg
{
    public function existEchostr(){
        if(!isset($_GET["echostr"]))
        {
            //调用响应消息函数
            $this->responseMsg();      
        }
        else
        {
            //实现网址接入，调用验证消息函数
            $this->valid();  
        }
    }
    
    //验证消息
    public function valid(){
        if($this->checkSignature())
        {
            $echostr =input('param.echostr');
            echo $echostr;
            exit;
        }
        else
        {
            echo "error";
            exit;
        }
    }
    
    
    //检查签名
    public function checkSignature()
    {
        //获取微信服务器GET请求的4个参数
        $all=input('param.');
        $signature = input('param.signature');
        $timestamp = input('param.timestamp');
        $nonce = input('param.nonce');
        
        //定义一个数组，存储其中3个参数，分别是timestamp，nonce和token
        $tempArr = array($nonce,$timestamp,config('Token'));
        
        //进行排序
        sort($tempArr,SORT_STRING);
        
        //将数组转换成字符串
        
        $tmpStr = implode($tempArr);
        
        //进行sha1加密算法
        $tmpStr = sha1($tmpStr);
        
        //判断请求是否来自微信服务器，对比$tmpStr和$signature
        if($tmpStr == $signature)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    
}

